﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using game_for_test_helper.src.graphics;
using Microsoft.Xna.Framework.Input;
using game_for_test_helper.src.helper;

namespace game_for_test_helper.GameTest
{
    class GameTest3 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Vector3 pointpos;
        LineManger lineManger;
        PointManger pointMange;
        SimpleCamera simpleCamera;
        GameStaticHolder gameStaticHolder;
        ModelCreator rocketmodel;
        TextWriteHelper TextWriteHelper;
        SceneLoader sceneLoader;

        SpriteFont font;
        public GameTest3()
        {
            graphics = new GraphicsDeviceManager(this);
            lineManger = new LineManger(this);
            pointMange = new PointManger(this);
            TextWriteHelper = new TextWriteHelper(this);
            simpleCamera = new SimpleCamera(this);
            gameStaticHolder = new GameStaticHolder(this);
            sceneLoader = new SceneLoader(this);

            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 853;
            graphics.PreferredBackBufferHeight = 480;

            graphics.MinimumVertexShaderProfile = ShaderProfile.VS_2_0;
            graphics.MinimumPixelShaderProfile = ShaderProfile.PS_2_0;

            Components.Add(TextWriteHelper);
            Components.Add(lineManger);
            Components.Add(pointMange);
            Components.Add(simpleCamera);
            Components.Add(gameStaticHolder);
            Components.Add(sceneLoader);
            //Components.add
        }
        protected override void Initialize()
        {
            base.Initialize();
            pointpos = new Vector3(.0f, .0f, .0f);
            Posrocketmodel = new Vector3(.0f, .0f, .0f);
        }
       
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("Fonts/SpriteFont");
            rocketmodel = new ModelCreator("models/Silithidwasp");
//            rocketmodel.doAction("Walk",true);
        }
        protected override void UnloadContent()
        {
        }
        private ButtonState Bstate;
        int step = 0;
        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X != 0)
                Posrocketmodel.X += GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X;
            if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y!=0)
                Posrocketmodel.Y += GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y;
            if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
                GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
            if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
            {
                Bstate = ButtonState.Pressed;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Released)
            {
                if (Bstate==ButtonState.Pressed)
                {
                    int co = rocketmodel.TakeConfigurer.getTakesCount();
                    Bstate = ButtonState.Released;
                    step++;
                    step=step%(co+1);
                    if (step != co)
                    {
                        rocketmodel.doAction(rocketmodel.TakeConfigurer.getTake(step).Clipname,true);
                    }
                    else rocketmodel.modelInit();
                }
            }
            //if (rocketmodel.WheatherCollisionScene())
            //    ModelCollisionMessage = "Yes";
            //else
            //    ModelCollisionMessage = "No";
            rocketmodel.update(gameTime);
            base.Update(gameTime);
        }
        string ModelCollisionMessage;
        Vector3 Posrocketmodel;
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            lineManger.drawline(new Vector3(500, 0, 0), Vector3.Zero, Color.Red);
            lineManger.drawline(new Vector3(500, 0, 20), new Vector3(0, 0, 20), Color.Red);
            lineManger.drawline(new Vector3(500, 0, 40), new Vector3(0, 0, 40), Color.Red);
            lineManger.drawline(new Vector3(0, 500, 20), new Vector3(0, 0, 20), Color.Blue);
            lineManger.drawline(new Vector3(0, 500, 40), new Vector3(0, 0, 40), Color.Blue);
            lineManger.drawline(new Vector3(0, 500, 0), Vector3.Zero, Color.Blue);
            lineManger.drawline(new Vector3(0, 0, 500), Vector3.Zero, Color.Black);
            rocketmodel.Render(Matrix.CreateTranslation(Posrocketmodel));           
            pointpos = simpleCamera.CameraPostion;
            pointMange.DrawPoint(Vector3.Zero);
            string MyMessage = pointpos.ToString();
            if (rocketmodel.PlayingCurrentClip1!=null)
             MyMessage = rocketmodel.PlayingCurrentClip1.Duration.ToString();
            TextWriteHelper.Write(new Vector2(50.0f, 60.0f), MyMessage);
            TextWriteHelper.Write(new Vector2(50.0f, 100.0f), ModelCollisionMessage);
            //spriteBatch.Begin();
            //string MyMessage = pointpos.ToString();
            //spriteBatch.DrawString(font, MyMessage, new Vector2(50.0f, 60.0f), Color.Black);
            //spriteBatch.End();
            //spriteBatch.Begin(SpriteBlendMode.None);
            //spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
